package com.itboye.jmsfhz.app.login.ui;

import java.util.Observable;
import java.util.Observer;

import android.app.AlertDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.jmsfhz.R;
import com.itboye.jmsfhz.app.ui.main.MainPresenter;
import com.itboye.jmsfhz.data.presenter.GetYanzhengMaPresenter;

public class FindBackPasswordActivity extends BaseActivity implements Observer {

	EditText yanzhengma;
	Button tv_get;
	EditText new_passwpord;
	Button find_password_bt;
	String phoneNumber;
	GetYanzhengMaPresenter getYanzhengMaPresenter;
	String codeType = 2 + "";
	private String code;
	MainPresenter mainPresenter;
	protected String newPwd;
	ImageView back;
	TextView txt_title;
	ImageView img_other;
	TimeCount count;
	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_find_password;
	}

	@Override
	public void onMyCreate() {
		txt_title.setText("找回密码");
		img_other.setVisibility(View.GONE);
		count=new TimeCount(60000, 1000, tv_get);
		getData();
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		tv_get.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				getYanzhengMaPresenter.getVerificationCode(phoneNumber,
						codeType);
				count.start();
			}
		});
		find_password_bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				newPwd = new_passwpord.getText().toString().trim();
				if (newPwd == null) {
					Toast.makeText(FindBackPasswordActivity.this, "请输入新的密码", 0)
							.show();
				} else {

					mainPresenter.updatePasswordByTelephone(code, newPwd,
							phoneNumber);
				}
			}
		});
	}

	private void getData() {
		mainPresenter = new MainPresenter(this);
		getYanzhengMaPresenter = new GetYanzhengMaPresenter(this);
		phoneNumber = getIntent().getStringExtra("phoneNumber");
	}

	@Override
	public void update(Observable arg0, Object arg1) {

		ResultEntity entity = handlerError(arg1);
		if (entity.getEventType() == MainPresenter.UpdatePwdByPhoneSuccessEvent) {
			Toast.makeText(FindBackPasswordActivity.this, "密码找回成功", 0).show();
			finish();
		} else if (entity.getEventType() == GetYanzhengMaPresenter.YanzhengmaSuccessEvent) {
			code = entity.getData().toString();
			yanzhengma.setText(code);
		}
	}

}
